#include "AstroidSimul.h"

#include "CoerciveFieldAlgo.h"
#include "AstroidSave.h"
#include "Output.h"
#include "TrajSimul.h"
#include "NewParameters.h"

#include <string>
#include <sstream>
#include <iostream>

using namespace std;

AstroidSimul::AstroidSimul()
{

}

AstroidSimul::~AstroidSimul()
{

}

MultiLayer AstroidSimul::solve(Clock clock,Output output,Pulse *field,Pulse *current,MultiLayer multiLayer,NewParameters params)
{
  int layerNumber=multiLayer.getSolveLayer();
  output.setSave(false);
  AstroidSave().initialize(output);
  MultiLayer downMultiLayer=multiLayer;
  Vector3 downMag=multiLayer[layerNumber].getMag();
  downMag.mult(-1);
  downMultiLayer[layerNumber].setMag(downMag);

  for(field->setLevel(params.getField()["min"]);field->getLevel(clock.getTimeStamp())<=params.getField()["max"];field->setLevel(params.getField()["step"]+field->getLevel(clock.getTimeStamp())))
    {
      for(field->setAngle(params.getField()["thetaMin"]);field->getAngle()<=params.getField()["thetaMax"];field->setAngle(field->getAngle()+params.getField()["thetaStep"]))
	{  
	  Vector3 smallAngle;
	  smallAngle.setVector(0,1E-5,-1E-5);
	  smallAngle.add(multiLayer[layerNumber].getMag());
	  multiLayer[layerNumber].setMag(smallAngle);
	  MultiLayer up=TrajSimul().solve(clock,output,field,current,multiLayer,params);
	  smallAngle.setVector(0,1E-5,-1E-5);
	  smallAngle.add(downMultiLayer[layerNumber].getMag());
	  downMultiLayer[layerNumber].setMag(smallAngle);
	  MultiLayer down=TrajSimul().solve(clock,output,field,current,downMultiLayer,params);
	  downMag=down[layerNumber].getMag();
	  downMag.add(up[layerNumber].getMag());
	  downMag.mult(0.5);
	  up[layerNumber].setMag(downMag);
	  AstroidSave().save(clock,output,field,up[layerNumber]);
	}
    }
  return multiLayer;
}
